package cn.cyl.product.service.impl;

import cn.cyl.basic.service.BaiduAiAuditService;
import cn.cyl.basic.service.impl.BaseServiceImpl;
import cn.cyl.basic.util.AjaxResult;
import cn.cyl.product.domain.Product;
import cn.cyl.product.domain.ProductDetail;
import cn.cyl.product.domain.ProductOnlineAuditLog;
import cn.cyl.product.mapper.ProductDetailMapper;
import cn.cyl.product.mapper.ProductMapper;
import cn.cyl.product.mapper.ProductOnlineAuditLogMapper;
import cn.cyl.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private BaiduAiAuditService baiduAiAuditService;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;


    /**
     * 产品-产品详情连表操作
     * @param product
     */
    @Transactional
    @Override
    public void save(Product product) {
        //添加一个产品
        super.save(product);
        //获得产品详情
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail!=null){//如果获取到的有值
            //就将产品的ID 设置到产品类型的Id
            productDetail.setProductId(product.getId());
            //添加进数据库
            productDetailMapper.save(productDetail);
        }
    }

    /**
     * 产品-产品详情连表操作
     * @param product
     */
    @Transactional
    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail!=null){
            //就将产品的ID 设置到产品类型的Id
            productDetail.setProductId(product.getId());
            //修改数据库
            productDetailMapper.updateByProductId(productDetail);
        }
    }

    /**
     * 产品-产品详情连表操作
     * @param id
     */
    @Transactional
    @Override
    public void del(Long id) {
        //根据产品id删
        productDetailMapper.delByProductId(id);
        super.del(id);
    }

    /**
     * 产品-产品详情连表操作
     * @param ids
     */
    @Transactional
    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }


    /**
     * 上架
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            Product product = productMapper.loadOne(id);
            //ai审核
            Boolean boolean1 = baiduAiAuditService.textAudit(product.getName());
            Boolean boolean2 = baiduAiAuditService.imageAudit(product.getResources());
            if(boolean1 && boolean2){//审核通过
                productMapper.updateOnsaleState(id,new Date());
                //添加审核日志
                ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
                productOnlineAuditLog.setProductId(id);
                productOnlineAuditLog.setState(1);
                productOnlineAuditLog.setNote("审核成功");
                productOnlineAuditLog.setAuditTime(new Date());
                productOnlineAuditLogMapper.save(productOnlineAuditLog);
            }else {
                //添加审核日志
                ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
                productOnlineAuditLog.setProductId(id);
                productOnlineAuditLog.setState(0);
                productOnlineAuditLog.setNote("审核失败");
                productOnlineAuditLog.setAuditTime(new Date());
                productOnlineAuditLogMapper.save(productOnlineAuditLog);
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.setSuccess(false);
                ajaxResult.setMsg(product.getName()+"审核失败");
                return ajaxResult;
            }
        }
        return new AjaxResult();
    }


    /**
     * 下架
     * @param ids
     */
    @Transactional
    @Override
    public void offsale(List<Long> ids) {
        //修改状态state=0   设置下架时间
        productMapper.updateOffsale(ids,new Date());

    }
}
